package com.zhr.tea.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.zhr.tea.R;
import com.zhr.tea.api.FenLeiApi;
import com.zhr.tea.entity.FenLeiContent;
import com.zhr.tea.entity.FenLeiHead;
import com.zhr.tea.view.activity.FenLeiActivity;
import com.zhr.tea.view.activity.SearchActivity;
import com.zhr.tea.view.adapter.FenleiAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import okhttp3.Headers;


public class FenLeiFragment extends Fragment implements AdapterView.OnItemClickListener ,View.OnClickListener{


    private ListView fenleiListview;
    private ArrayAdapter<String> adapter;
    private List<FenLeiHead.ContentBean> contentBean = new ArrayList<>();
    private List<String> data = new ArrayList<>();
    private EditText editText;

    public FenLeiFragment() {
        // Required empty public constructor
    }

    public static FenLeiFragment newInstance() {

        Bundle args = new Bundle();

        FenLeiFragment fragment = new FenLeiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fen_lei, container, false);
        fenleiTv = (TextView) view.findViewById(R.id.fenlei_tv);
        recy = (RecyclerView) view.findViewById(R.id.fenlei_recy);
        fenleiTv.setText("品类");
        fenleiListview = (ListView) view.findViewById(R.id.fenlei_listview);
        editText = (EditText) view.findViewById(R.id.editText);
        editText.setOnClickListener(this);
        initView();
        getListData();
        getContentData();
        addListener();
        return view;
    }

    /*****
     * 给RecycleView添加点击事件
     */
    private void addListener() {
        madapter.setOnItemClickListener(new FenleiAdapter.RecyClickListener() {
            @Override
            public void onItemClick(String title) {
                Intent intent = new Intent(getActivity(), FenLeiActivity.class);
                intent.putExtra("title", title);
                startActivity(intent);
            }
        });

    }


    private List<FenLeiContent.ContentBean.SubClassListBean> subClassListBean;
    private FenleiAdapter madapter;
    private TextView fenleiTv;
    private RecyclerView recy;

    private void initView() {
        adapter = new ArrayAdapter<String>(getContext(), R.layout.item_fenlei_lv_tv, data);
        fenleiListview.setAdapter(adapter);
        fenleiListview.setOnItemClickListener(this);
        subClassListBean = new ArrayList<>();
        RecyclerView.LayoutManager manager = new GridLayoutManager(getContext(), 2);
        recy.setLayoutManager(manager);
        madapter = new FenleiAdapter(getContext(), subClassListBean);
        recy.setAdapter(madapter);


    }

    private void getListData() {
        HttpRequest.get(FenLeiApi.FENLEI_TAB, new BaseHttpRequestCallback<String>() {
            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            protected void onSuccess(Headers headers, String s) {
                super.onSuccess(headers, s);
                String json = s.substring(s.indexOf("(") + 1, s.lastIndexOf(")"));
                FenLeiHead head = JSONObject.parseObject(json, FenLeiHead.class);
                for (int i = 0; i < head.getContent().size(); i++) {
                    contentBean.add(new FenLeiHead.ContentBean(head.getContent().get(i).getClassId(), head.getContent().get(i).getClassName()));
                    data.add(contentBean.get(i).getClassName());
                }
                adapter.notifyDataSetChanged();
            }
        });
    }


    private void getContentData() {
        HttpRequest.get(getUrl(), new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(Headers headers, String s) {
                super.onSuccess(headers, s);
                String json = s.substring(s.indexOf("(") + 1, s.lastIndexOf(")"));
                FenLeiContent fenLeiContent = JSONObject.parseObject(json, FenLeiContent.class);
                subClassListBean.addAll(fenLeiContent.getContent().getSubClassList());
                madapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }
        });
    }


    private String getUrl() {

        return FenLeiApi.FENLEI_CONTENT + id;
    }

    private String id = "10";

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        subClassListBean.clear();
        id = contentBean.get(i).getClassId() + "";
        getContentData();
    }


    @Override
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }
}